package fun.util.daohelper;

import fun.util.daohelper.expression.SearchExpression;

import java.util.List;

/**
 * Author: bin
 * Date: Jan 9, 2006
 * Time: 10:34:34 PM
 */
public class AbstractDao<D,K extends PrimaryKey> implements Dao<D,K> {
    private Class<? extends D> dataType;
    private DaoHelper<D> daoHelper;

    protected AbstractDao( DaoHelper<D> daoHelper, Class<? extends D> dataType )  {
        this.daoHelper = daoHelper;
        this.dataType = dataType;
    }


    public void updateBulk( D... data) {
        daoHelper.save( data );
    }

    public D update(D data) {
        daoHelper.save( data );
        return data;
    }

    public D findByPk( K key) {
        return daoHelper.find( dataType, key.getValue() );
    }

    public void remove( D data ) {
        daoHelper.remove( data );
    }


    public D removeByPk(K key ) {
        return daoHelper.removeById( dataType, key.getValue() );
    }


    protected D findUniqueByFields( SearchExpression... parameters ) {
        return getDaoHelper().findUniqueByFields( dataType, parameters );
    }

    protected List<D> findByFields( SearchExpression... parameters) {
        return getDaoHelper().findByFields( dataType, parameters );
    }

    protected int countByFields( SearchExpression... parameters ) {
        return getDaoHelper().countByFields( dataType, parameters );
    }

    protected DaoHelper<D> getDaoHelper() {
        return daoHelper;
    }
}
